package fenyu.personblob.bean.strategy.blob;

import fenyu.personblob.bean.databean.User;
import fenyu.personblob.bean.databean.blob.Blob;
import fenyu.personblob.bean.databean.blob.BlobComment;
import fenyu.personblob.bean.strategy.Strategy;
import fenyu.personblob.error.NotFoundError;
import fenyu.personblob.single.SinglePathDoor;

/**
 * create at 2018/12/22 11:22
 *
 * @author 纷羽 <gzj0078@gmail.com>
 */
public class SendBlobCommentStrategy implements Strategy {
    @Override
    public Object todo(Object... args) {
        User user = (User) args[0];
        Integer blob_id = (Integer) args[1];
        String text = (String) args[2];
        Integer comment_id = null;
        if (args.length > 3)
            comment_id = (Integer) args[3];
        Blob blob = SinglePathDoor.getSingleSave().getSaveDoor().getBlobSavePath().getById(blob_id);
        if (blob == null)
            throw new NotFoundError();
        BlobComment commenta = null;
        if (comment_id != null) {
            commenta = SinglePathDoor.getSingleSave().getSaveDoor().getBlobCommentSavePath().getById(comment_id);
            if (commenta == null)
                throw new NotFoundError();
        }
        BlobComment comment = (BlobComment) SinglePathDoor.getSingleFuns().getAutoFactory().createBlobComment(null);
        comment.setBlob(blob_id);
        comment.setSend_user(user.getId());
        comment.setText(text);
        comment = SinglePathDoor.getSingleSave().getSaveDoor().getBlobCommentSavePath().save(comment);
        if (commenta != null) {
            commenta.getBlobComments().add(comment.getId());
        }
        return comment;
    }
}
